package com.example.wms;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import com.example.classes.*;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Default Activity that starts the Login screen.
 */
public class StartupActivity extends Activity
{
    public final static String USER_ID = "com.example.wms.UNAME";
    public final static String PASS = "com.example.wms.UPASS";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DataBase db = new DataBase(this);
        db.open();
        Collection<Member> listofMembers = db.getAllUsers();

        boolean hasAdmin = false;
        for(Member m: listofMembers)
        {
            if (m.getName().equals("admin"))
                hasAdmin = true;
            Log.v("WMS-DEBUG","STARTUP: " + m.toString());
        }

        if (!hasAdmin)
        {
            String upass = Encryptor.encrypt("admin");
            String email = "default.admin@gatech.edu";
            Admin newAdmin = new Admin("admin", upass, email);
            db.createUserEntry(newAdmin);

            Log.v("WMS-DEBUG", "STARTUP: Made the default admin account.");
            Log.v("WMS-DEBUG", "STARTUP: " + newAdmin.toString());
        }

        // -- only uncommment if the database has been deleted
        // -- this will populate the database with test items

        /*Admin newAdmin = new Admin("Alex", Encryptor.encrypt("go"), "a.huynh@gatech.edu");

        Location locN = new Location("Dummy");
        locN.setLatitude(33.65);
        locN.setLongitude(84.42);
        Item iN = new Item("Near", "An object right here.", "Heirloom", locN, true, 5.0, new Date(), null, newAdmin);
        newAdmin.addItem(iN);

        Location locM = new Location("Dummy");
        locM.setLatitude(16);
        locM.setLongitude(40);
        Calendar cal = GregorianCalendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -4);
        Date recent = cal.getTime();
        Item iM = new Item("Not-so-near", "An object far away.", "Keepsake", locM, true, 10.0, recent, null, newAdmin);
        newAdmin.addItem(iM);

        Location locF = new Location("Dummy");
        locF.setLatitude(0);
        locF.setLongitude(0);
        cal.add(Calendar.DAY_OF_YEAR, -16);
        Date old = cal.getTime();
        Item iF = new Item("Far", "An object really far away.", "Misc", locF, true, 20.0, old, null, newAdmin);
        newAdmin.addItem(iF);

        db.createUserEntry(newAdmin);
        Log.v("WMS-DEBUG", "STARTUP: Made the Location-testing items.");
        Log.v("WMS-DEBUG", "STARTUP: " + newAdmin.toString()); */

        db.close();
    }

    /**
     * Attempts to login with the inputted credentials.
     * @param view the content view
     */
    public void tryLogin(View view)
    {
        Intent intent = new Intent(this, LoginHandler.class);
        EditText loginID = (EditText)findViewById(R.id.uid_enter);
        EditText pass = (EditText) findViewById(R.id.upass_enter);
        intent.putExtra(USER_ID, loginID.getText().toString() );
        intent.putExtra(PASS, pass.getText().toString());
        startActivity(intent);
    }

    /**
     * Called when the "Register" button is pressed. Opens the registration screen.
     * @param view the content view
     */
    public void registerClicked(View view)
    {
        Intent intent = new Intent(this, registerActivity.class);
        intent.putExtra("makeAdmin", false);
        startActivity(intent);
    }
}
